//
//  MeViewController.m
//  Article
//
//  Created by LONG on 16/4/21.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MeViewController.h"
#import "MeTableViewCell.h"
#import "MyInfoViewController.h"
#import "InfoViewController.h"
#import "CashViewController.h"
#import "FundDataViewController.h"
#import "InviteFriendsViewController.h"
#import "AboutViewController.h"
#import "HelpCenterViewController.h"

#import "UIView+Toast.h"
#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"
#import "UIKit+AFNetworking.h"
#import "AFNetworking.h"
#import "MessageCenterViewController.h"
@interface MeViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)UIImageView *headImageView;//头像
@property (nonatomic,strong)UILabel *nameLabel;//昵称

@property (nonatomic,strong)MyInfoViewController *myInfoVC;

@end

@implementation MeViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
    [_myInfoVC returnInfoBlock:^(UIImage *headImage, NSString *nameLabelStr) {
        
        _headImageView.image = headImage;
        _nameLabel.text = nameLabelStr;
        
    }];
    
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(0xff6a6a)];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _myInfoVC = [[MyInfoViewController alloc]init];
    _dataArray = [NSMutableArray array];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initNavigation];
    
    [self createUI];
    
}
-(void)initNavigation{
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.navigationItem.title = @"";
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
    
    //nav底部线消失
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
    //    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"消息" style:UIBarButtonItemStylePlain target:self action:@selector(message)];
    //    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:15],NSForegroundColorAttributeName:UIColorFromRGB(0xffffff)} forState:UIControlStateNormal];
    
}
-(void)message{
    NSLog(@"点我进入消息页面");
    MessageCenterViewController *messageVC = [[MessageCenterViewController alloc]init];
    [self.navigationController pushViewController:messageVC animated:YES];
}

-(void)createUI{
    
    NSUserDefaults *user = [[NSUserDefaults  alloc]init];
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 150 *MYHEIGHT)];
    headView.backgroundColor = UIColorFromRGB(0xff6a6a);
    headView.userInteractionEnabled = YES;
    [self.view addSubview:headView];
    
    _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(295/2 *MYWIDTH, 0, 80*MYHEIGHT, 80 *MYHEIGHT)];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:[user objectForKey:@"imageurl"]] placeholderImage:[UIImage imageNamed:@"头像"]];
    _headImageView.layer.cornerRadius = 40 *MYHEIGHT;
    _headImageView.layer.masksToBounds = YES;
    _headImageView.userInteractionEnabled = YES;
    [headView addSubview:_headImageView];
    
    
    UITapGestureRecognizer *headTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headClick:)];
    [_headImageView addGestureRecognizer:headTap];
    
    
    _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(175/2 *MYWIDTH, 80 *MYHEIGHT, 200 *MYWIDTH, 40)];
    _nameLabel.text = [user objectForKey:@"nickname"];
    _nameLabel.textColor = [UIColor whiteColor];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.font = [UIFont fontWithName:@"Heiti SC" size:14 *MYWIDTH];
    [headView addSubview:_nameLabel];
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49 - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MeIDXIB"];
    
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = headView;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
-(void)headClick:(UITapGestureRecognizer *)headTap{
    [self.navigationController pushViewController:_myInfoVC animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MeIDXIB"];
    if (indexPath.row == 0) {
        cell.textLabel.text = @"个人资料";
        UIImage *icon = [UIImage imageNamed:@"邀请好友"];
        CGSize itemSize = CGSizeMake(25, 25);
        UIGraphicsBeginImageContextWithOptions(itemSize, NO,0.0);
        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        [icon drawInRect:imageRect];
        cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        //        MeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeIDXIB" forIndexPath:indexPath];
        //        [cell.cashBtn addTarget:self action:@selector(cashClick:) forControlEvents:UIControlEventTouchUpInside];
        //        [cell.infoBtn addTarget:self action:@selector(infoClick:) forControlEvents:UIControlEventTouchUpInside];
        //        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //        return  cell;
        //    }if (indexPath.row == 1) {
        //        cell.textLabel.text = @"邀请好友";
        //        UIImage *icon = [UIImage imageNamed:@"邀请好友"];
        //        CGSize itemSize = CGSizeMake(25, 25);
        //        UIGraphicsBeginImageContextWithOptions(itemSize, NO,0.0);
        //        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        //        [icon drawInRect:imageRect];
        //        cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        //        UIGraphicsEndImageContext();
        //    }if (indexPath.row == 2) {
        //        cell.textLabel.text = @"资金日志";
        //        UIImage *icon = [UIImage imageNamed:@"资金日志"];
        //        CGSize itemSize = CGSizeMake(25, 25);
        //        UIGraphicsBeginImageContextWithOptions(itemSize, NO,0.0);
        //        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        //        [icon drawInRect:imageRect];
        //        cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        //        UIGraphicsEndImageContext();
        
//    }if (indexPath.row == 2) {
//        cell.textLabel.text = @"帮助中心";
//        UIImage *icon = [UIImage imageNamed:@"帮助中心"];
//        CGSize itemSize = CGSizeMake(25, 25);
//        UIGraphicsBeginImageContextWithOptions(itemSize, NO,0.0);
//        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
//        [icon drawInRect:imageRect];
//        cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
//        UIGraphicsEndImageContext();
    }if (indexPath.row == 1) {
        cell.textLabel.text = @"关于我们";
        UIImage *icon = [UIImage imageNamed:@"联系方式"];
        CGSize itemSize = CGSizeMake(25, 25);
        UIGraphicsBeginImageContextWithOptions(itemSize, NO,0.0);
        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        [icon drawInRect:imageRect];
        cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    //    if (indexPath.row == 1) {
    //        cell.textLabel.text = @"消息中心";
    //        UIImage *icon = [UIImage imageNamed:@"套餐购买"];
    //        CGSize itemSize = CGSizeMake(25, 25);
    //        UIGraphicsBeginImageContextWithOptions(itemSize, NO,0.0);
    //        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    //        [icon drawInRect:imageRect];
    //        cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    //        UIGraphicsEndImageContext();
    //    }
    
    cell.textLabel.textColor = UIColorFromRGB(0x333333);
    cell.textLabel.font = [UIFont fontWithName:@"Heiti SC" size:13 *MYWIDTH];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 50;
    }else{
        return 50;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 0) {
        NSLog(@"个人资料");
        
        [self.navigationController pushViewController:_myInfoVC animated:YES];
        
        //        NSLog(@"邀请好友");
        //        InviteFriendsViewController *inviteVC = [[InviteFriendsViewController alloc]init];
        //        [self.navigationController pushViewController:inviteVC animated:YES];
        //    }if (indexPath.row == 2) {
        //        NSLog(@"资金日志");
        //        FundDataViewController *FundDataVC = [[FundDataViewController alloc]init];
        //        [self.navigationController pushViewController:FundDataVC animated:YES];
        
    }
//    if (indexPath.row == 2) {
//        NSLog(@"帮助中心");
//        HelpCenterViewController *helpCenterVC = [[HelpCenterViewController alloc]init];
//        [self.navigationController pushViewController:helpCenterVC animated:YES];
//        
//    }
    if (indexPath.row == 1) {
        NSLog(@"关于我们");
        AboutViewController *aboutVC = [[AboutViewController alloc]init];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
//    }if (indexPath.row == 1) {
//        NSLog(@"消息中心");
//        MessageCenterViewController *messageVC = [[MessageCenterViewController alloc]init];
//        [self.navigationController pushViewController:messageVC animated:YES];
//    }
    
}
-(void)cashClick:(UIButton *)btn{
    NSLog(@"提现");
    CashViewController *cashVC = [[CashViewController alloc]init];
    [self.navigationController pushViewController:cashVC animated:YES];
}
-(void)infoClick:(UIButton *)btn{
    NSLog(@"详情");
    InfoViewController *InfoVC = [[InfoViewController alloc]init];
    [self.navigationController pushViewController:InfoVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
